package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.common.common.SystemDateFormatUtil;
import com.aier.school.entity.SchoolMessage;



public class SchoolMessageRowMapper implements RowMapper<SchoolMessage> {

    private int type;
    
    public static final int AND_TEACHERNAME=1;
    public static final int AND_TEACHER=2;

    public SchoolMessageRowMapper() {
    }

    public SchoolMessageRowMapper(int type) {
        this.type = type;
    }

    @Override
    public SchoolMessage mapRow(ResultSet rs, int rowNum) throws SQLException {
        SchoolMessage sm = parseSchoolMessage(new SchoolMessage(), rs);
        sm.setCtime_str(SystemDateFormatUtil.formatDate(sm.getCtime(), SystemDateFormatUtil.FORMAT_DATETIME_NOTSECOND));

        switch (type) {
            case AND_TEACHERNAME:
                sm.setName_teacher(rs.getString("name_teacher"));
                sm.setHeadimg_teacher(rs.getString("headimg_teacher"));
                sm.setSuccessStr(rs.getString("successStr"));
                sm.setFailStr(rs.getString("failStr"));
                break;
            case AND_TEACHER:
                sm.setName_teacher(rs.getString("name_teacher"));
                break;
        }
        return sm;
    }

    public static SchoolMessage parseSchoolMessage(SchoolMessage s, ResultSet rs) throws SQLException {
        return parseSchoolMessage(s, rs, null);
    }

    public static SchoolMessage parseSchoolMessage(SchoolMessage s, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            s.setMid(rs.getLong(su+"mid"));
        } catch (SQLException e) {
            s.setMid(null);
        }
        try {
            s.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            s.setSid(null);
        }
        try {
            s.setCid(rs.getLong(su+"cid"));
        } catch (SQLException e) {
            s.setCid(null);
        }
        try {
            s.setTid(rs.getLong(su+"tid"));
        } catch (SQLException e) {
            s.setTid(null);
        }
        try {
            s.setMessage(rs.getString(su+"message"));
        } catch (SQLException e) {
            s.setMessage(null);
        }
        try {
            s.setTitle(rs.getString(su+"title"));
        } catch (SQLException e) {
            s.setTitle(null);
        }
        try {
            s.setCtime(rs.getTimestamp(su+"ctime"));
        } catch (SQLException e) {
            s.setCtime(null);
        }
        try {
            s.setType(rs.getShort(su+"type"));
        } catch (SQLException e) {
            s.setType(null);
        }
        try {
            s.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            s.setImg(null);
        }
        try {
            s.setCount(rs.getInt(su+"count"));
        } catch (SQLException e) {
            s.setCount(null);
        }
        try {
            s.setReading(rs.getInt(su+"reading"));
        } catch (SQLException e) {
            s.setReading(null);
        }
        return s;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, SchoolMessage s) {
        par.put("mid", s.getMid());
        par.put("sid",s.getSid());
        par.put("cid",s.getCid());
        par.put("tid", s.getTid());
        par.put("message", s.getMessage());
        par.put("ctime",s.getCtime());
        par.put("type",s.getType());
        par.put("img",s.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, SchoolMessage s) {
        args.put("mid", s.getMid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final SchoolMessage s) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(s),
                        new String[] { "mid" });
                parsePS(ps, s);
                return ps;
            }

        };
    }

    private static String parseSQL(SchoolMessage s) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into school_message(");
        int count = 0;
        if (null != s.getSid()) {
            sql.append("sid,");
            count++;
        }

        if (null !=s.getCid()) {
            sql.append("cid,");
            count++;
        }
        if (null !=s.getTid()) {
            sql.append("tid,");
            count++;
        }
        if (null !=s.getMessage()) {
            sql.append("message,");
            count++;
        }
        if (null != s.getCtime()) {
            sql.append("ctime,");
            count++;
        }
        if (null != s.getType()) {
            sql.append("type,");
            count++;
        }
        if (null != s.getImg()) {
            sql.append("img,");
            count++;
        }
        if (null != s.getTitle()) {
            sql.append("title,");
            count++;
        }
        if (null != s.getCount()) {
            sql.append("count,");
            count++;
        }
        if (null != s.getReading()) {
            sql.append("reading,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, SchoolMessage s)
        throws SQLException {
        int count = 1;
        if (null != s.getSid()) {
            ps.setInt(count++,s.getSid());
        }
        if (null != s.getCid()) {
            ps.setLong(count++, s.getCid());
        }
        if (null != s.getTid()) {
            ps.setLong(count++, s.getTid());
        }
        if (null != s.getMessage()) {
            ps.setString(count++, s.getMessage());
        }
        if (null != s.getCtime()) {
            ps.setTimestamp(count++,s.getCtime());
        }
        if (null != s.getType()) {
            ps.setShort(count++,s.getType());
        }
        if (null != s.getImg()) {
            ps.setString(count++,s.getImg());
        }        
        if (null != s.getTitle()) {
            ps.setString(count++,s.getTitle());
        }
        if (null != s.getCount()) {
            ps.setInt(count++,s.getCount());
        }
        if (null != s.getReading()) {
            ps.setInt(count++,s.getReading());
        }
    }

}
